package com.douwong.student.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.ClassBaseActivity;
import com.douwong.student.contant.Config;

/**
 * @autor yichao
 * @time 2017/4/89:22
 * @des 锁屏界面
 */
public class LockActivity extends ClassBaseActivity {
    public static boolean isShow;
    @Override
    protected void initView() {
        setContentView(R.layout.lock_screen_layout);


    }

    @Override
    protected void initData() {
        super.initData();
        isShow=true;
        registerReceiver(mReceiver,mFilter);

    }

    IntentFilter  mFilter=new IntentFilter(){
        {
            addAction(Config.LOGIN_CLASS_FAILURE);
            addAction(Config.NO_LOCKSREEN);
            addAction(Config.TEACHER_LEAVE_CLASS);
        }
    };
    BroadcastReceiver mReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Config.LOGIN_CLASS_FAILURE :
                case Config.NO_LOCKSREEN:
                case Config.TEACHER_LEAVE_CLASS:
                    finish();

                    break;
            }

        }
    };
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if(BaseApplication.isLoginClass){
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    long firClick;
    long secClick;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            firClick = secClick;
            secClick = System.currentTimeMillis();
            if (secClick - firClick < 1000) {
                if(!BaseApplication.isLoginClass){
                    finish();
                }
            }
        }
        return super.onTouchEvent(event);
    }
    public  static  void startActivity(Context context){
        Intent intent=new Intent(context,LockActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isShow=false;
        if (mReceiver!=null){
            unregisterReceiver(mReceiver);
        }
    }
}
